package com.tang.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.constants.SystemConstants;
import com.tang.dto.CategoryDto;
import com.tang.enums.AppHttpCodeEnum;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Article;
import com.tang.sys.entity.Category;
import com.tang.sys.mapper.CategoryMapper;
import com.tang.sys.service.IArticleService;
import com.tang.sys.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.sys.vo.CategoryVo;
import com.tang.sys.vo.PageVo;
import com.tang.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author shankaka
 * @since 2023-04-14
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private IArticleService iArticleService;

    @Autowired
    private ICategoryService iCategoryService;

    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public ResponseResult getCategoryList() {
        //查询文章表 状态为发布得文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = iArticleService.list(articleWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //查询分类表
        List<Category> categories = listByIds(categoryIds);
        categories = categories.stream().
                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }
    //查询所有分类接口
    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus,SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list,CategoryVo.class);

        return categoryVos;
    }

    //分类的分页
    @Override
    public ResponseResult<PageVo> pageCategoryList(Integer pageNum, Integer pageSize, CategoryDto categoryDto) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(categoryDto.getStatus()),Category::getStatus,categoryDto.getStatus());
        wrapper.like(StringUtils.hasText(categoryDto.getName()),Category::getName,categoryDto.getName());

        Page<Category> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,wrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    //新增分类
    @Override
    public ResponseResult<CategoryVo> addCategory(CategoryVo categoryVo) {
        Category category = new Category();
        category.setName(categoryVo.getName());
        category.setDescription(categoryVo.getDescription());
        category.setStatus(categoryVo.getStatus());

        categoryMapper.insert(category);


        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(Long id) {

        this.categoryMapper.deleteById(id);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getInfoCategory(Long id) {
        Category byId = iCategoryService.getById(id);
        if (byId != null) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(byId, categoryVo);
            return ResponseResult.okResult(categoryVo);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("分类不存在"));
        }

    }

    @Override
    public ResponseResult<CategoryVo> updateCategory(CategoryVo categoryVo) {
        Category category = categoryMapper.selectById(categoryVo.getId());
        if (category == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("未找到"));
        }
        category.setName(categoryVo.getName());
        category.setDescription(categoryVo.getDescription());
        category.setStatus(categoryVo.getStatus());

        int i = categoryMapper.updateById(category);

        if (i == 1){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.valueOf("未找到"));
        }

    }

    @Override
    public List<CategoryVo> bloglistAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus,SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list,CategoryVo.class);

        return categoryVos;
    }

    @Override
    public ResponseResult updateCategoryStatus(Category category) {
        Category category1 = categoryMapper.selectById(category.getId());
        category1.setStatus(category.getStatus());
        categoryMapper.updateById(category1);

        return ResponseResult.okResult(200,"修改成功");

    }


}
